import tushare as ts
import pandas as pd
import numpy as np
import time
import datetime
from data_copy import newfile, copy_file
from qfq import qfq_func
# 获取每天个股数据

ts.set_token('883c1e2da92c0f68194fe2c4b4aa0e4da880b41ef62ff556f2da00e7')
pro = ts.pro_api()

#date_today = datetime.date.today().strftime('%Y%m%d')
date_today = '20190515'
df_data = pro.daily(trade_date=date_today)  # 获取每天个股数据
df_factor = pro.adj_factor(ts_code='', trade_date=date_today)  # 获取每天个股复权因子
start = time.clock()
# 数据筛选
for code in df_factor['ts_code'].values.tolist():
    if code not in df_data['ts_code'].values.tolist():
        print('删除factor表中多余的不在data表的元素:', code)  # 如B股退市股
        df_factor = df_factor.set_index(df_factor['ts_code'], drop=True)
        df_factor.drop(code, axis=0, inplace=True)
end = time.clock()
# 若factor表含有股票数据中所没有的股票，添加之
if len(df_data) != len(df_factor):
    print('factor表元素缺失，正在补齐...')
    for code in df_data['ts_code'].values.tolist():

        if code not in df_factor['ts_code'].values.tolist():
            print('添加数据:', code)
            df1 = pro.adj_factor(ts_code=code, trade_date=date_today)  # 获取缺失的股票当天的因子数据
            df1 = df1.set_index(df1['ts_code'], drop=True)  # 按代码排序
            df_factor = df_factor.append(df1)
end1 = time.clock()
df_factor.drop(['ts_code', 'trade_date'], axis=1, inplace=True)
df_data = df_data.set_index(df_data['ts_code'],drop=True).sort_index(level=0)
df_data = df_data.join(df_factor)

# 创建多层索引时间代码
df_data = df_data.set_index([df_data['trade_date'], df_data['ts_code']], drop=True)
df_data.drop('trade_date', axis=1, inplace=True)


# 提取保存的数据
stocks_data = pd.read_hdf('stock_data.h5', key='data')
stocks_data = stocks_data.append(df_data)

# 写入更新的数据
h5 = pd.HDFStore('stock_data.h5','w', complevel=4, complib='blosc')
h5['data'] = stocks_data
h5.close()

print('程序耗时：', end - start)
print('程序耗时：', end1 - end)
# 执行前复权函数
qfq_func()

# 文件备份

path = "C:\\Users\\ZQ_HHX\\PycharmProjects\\untitled4\\stockdata_copy\\"+date_today

newfile(path)
copy_file(date_today)


